﻿/*
 * FancyScrollView (https://github.com/setchi/FancyScrollView)
 * Copyright (c) 2020 setchi
 * Licensed under MIT (https://github.com/setchi/FancyScrollView/blob/master/LICENSE)
 */

using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using EasingCore;
using FancyScrollView;
using QFramework;
using UnityEngine.SceneManagement;
using Cysharp.Threading.Tasks;
using DG.Tweening;

class Cell : FancyCell<StageData>,IController
{
    readonly EasingFunction alphaEasing = Easing.Get(EasingCore.Ease.OutQuint);

    [SerializeField] Text title = default;
    [SerializeField] Text description = default;
    [SerializeField] RawImage image = default;
    [SerializeField] Image background = default;
    [SerializeField] CanvasGroup canvasGroup = default;
    [SerializeField] Button button = default;
    [SerializeField] RawImage finishImage = default;

    StageData data;

    private void Awake() 
    {
        button.onClick.AddListener(EnterGameAsync);
    }

    private void EnterGameAsync()
    {
        var audioSystem = this.GetSystem<AudioSystem>();
        audioSystem.PlaySoundEffect("Sound/effects/button04b");
        UIChooseStage.Instance.EnterGameAsync(data);
    }

    public override void UpdateContent(StageData itemData)
    {
        data = itemData;
        image.texture = null;

        if(itemData.IsFinish)
        {
            finishImage.gameObject.SetActive(true);
        }
        else
        {
            finishImage.gameObject.SetActive(false);
        }
        image.texture = itemData.Texture;
        title.text = itemData.Title;
        description.text = itemData.Description;

        if(itemData.NeedChange2Win)
        {
            ShowFinishImage().Forget();
            data.NeedChange2Win = false;
        }

        UpdateSibling();
    }

    public async UniTaskVoid ShowFinishImage()
    {
        await UniTask.Delay(500);
        
        finishImage.transform.localScale = Vector3.one * 10;
        finishImage.gameObject.SetActive(true);
        await DOTween.To(()=> finishImage.transform.localScale,x => finishImage.transform.localScale = x, Vector3.one * 0.4f, 1f)
        .SetEase(DG.Tweening.Ease.InQuad);
        var audioSystem = this.GetSystem<AudioSystem>();
        audioSystem.PlaySoundEffect("Sound/effects/short_punch1");
        data.IsFinish = true;
    }

    void UpdateSibling()
    {
        var cells = transform.parent.Cast<Transform>()
            .Select(t => t.GetComponent<Cell>())
            .Where(cell => cell.IsVisible);

        if (Index == cells.Min(x => x.Index))
        {
            transform.SetAsLastSibling();
        }

        if (Index == cells.Max(x => x.Index))
        {
            transform.SetAsFirstSibling();
        }
    }

    public override void UpdatePosition(float t)
    {
        const float popAngle = -15;
        const float slideAngle = 25;

        const float popSpan = 0.75f;
        const float slideSpan = 0.25f;

        t = 1f - t;

        var pop = Mathf.Min(popSpan, t) / popSpan;
        var slide = Mathf.Max(0, t - popSpan) / slideSpan;

        transform.localRotation = t < popSpan
            ? Quaternion.Euler(0, 0, popAngle * (1f - pop))
            : Quaternion.Euler(0, 0, slideAngle * slide);

        transform.localPosition = Vector3.left * 500f * slide;

        canvasGroup.alpha = alphaEasing(1f - slide);

        background.color = Color.Lerp(Color.gray, Color.white, pop);
    }

    public IArchitecture GetArchitecture() => MainArchitecture.Interface;
}

public class UniTaksVoid
{
}